package com.qikecn.update;

import android.app.Activity;
import android.os.Message;
import android.text.TextUtils;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by wangst on 16/6/24.
 */
public class UpdateHttpUtil {

    protected static final int SUCCESS = 200;
    protected static final int FAIL = 400;
    /**
     * 升级文件保存目录
     */
    protected static String ROOT_PATH;
    /**
     * 升级文件保存文件名称
     */
    protected static String UPDATE_FILE_NAME;
    
    /**
     * 升级过程回调接口
     */
    protected static Callback mCallback;


    private static ExecutorService service = Executors.newSingleThreadExecutor();


    /**
     * 升级检查
     * @param activity
     * @param url
     * @param isXml
     * @param root_path 升级文件保存的目录，默认：getFilesDir().toString() + File.pathSeparator
     * @param update_file_name 升级文件保存的文件名，默认：com.qikecn.update.apk
     * @param isShowMsg 是否显示升级信息
     */
    public static void checkNewVersion(final Activity activity, final String url, final boolean isXml, final String root_path, final String update_file_name, final Callback callback){
        
    	if(!TextUtils.isEmpty(root_path)){
            ROOT_PATH = root_path;
        }else {
            ROOT_PATH = activity.getFilesDir().toString() + File.separator;
        }
        if(!TextUtils.isEmpty(update_file_name)){
            UPDATE_FILE_NAME = update_file_name;
        }else {
            UPDATE_FILE_NAME = "com.qikecn.update.apk";
        }
        mCallback = callback;
        
        if(UpdateHttpUtil.mCallback != null){
    		UpdateHttpUtil.mCallback.onStart();
    	}
        
        final UpdateHandler handler = new UpdateHandler(activity);

        service.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    final String result = HttpClientUtil.simpleHttpGet(url);
                    System.out.println("checkNewVersion onSuccess result:" + result);
                    if (!TextUtils.isEmpty(result)) {
                        VersionBean version = null;
                        if (isXml) {
                            version = VersionObject.parseObjFromXml(result);
                        } else {
                            version = VersionObject.parseObjFromJson(result);
                        }
                        Message msg = Message.obtain(handler, SUCCESS, version);
                        msg.sendToTarget();
                    }else {
                    	if(UpdateHttpUtil.mCallback != null){
                    		handler.post(new Runnable() {
								
								@Override
								public void run() {
									// TODO Auto-generated method stub
									UpdateHttpUtil.mCallback.onFail("result is null:"+result);
								}
							});
                    	}
                    }
                } catch (final Exception e) {
                    e.printStackTrace();
                    if(UpdateHttpUtil.mCallback != null){
                    	handler.post(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								UpdateHttpUtil.mCallback.onFail(e.toString());
							}
						});
                	}
                }
            }
        });

    }
    
    public static interface Callback{
    	public void onStart();
    	public void onSuccess(String str, boolean discoveryNewVersion);
    	public void onFail(String str);
    }

}
